package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtil;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public boolean importData(List<String[]> list) {
        if(list != null){
            List<OrderSetting> dataList = new ArrayList<>();
            for (String[] row : list){
                String orderDate = row[0];
                String orderNumber = row[1];
                OrderSetting orderSetting = findByDate(orderDate);
                if(orderSetting == null){
                    orderSetting = new OrderSetting();
                }
                orderSetting.setOrderDate(DateUtil.parseDate(orderDate));
                orderSetting.setOrderNumber(Integer.parseInt(orderNumber));
                dataList.add(orderSetting);
            }
            this.saveOrUpdateBatch(dataList);
        }
        return true;
    }

    @Override
    public OrderSetting findByDate(String orderDate) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",orderDate);
        return this.getOne(queryWrapper);
    }

    @Override
    /**
     * {
     *         '2020-10-01':{number:200,reservations:200},
     *         '2020-10-02':{number:200,reservations:100},
     *         '2020-10-15':{number:200,reservations:0}
     *       }
     * */
    public Map<String, Map<String, Integer>> selectDataByMonth(int year, int month) {
        //条件构造
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        //mysql 按月查询 DATE_FORMAT
        // where DATE_FORMAT(order_date,'%Y-%m') = '2020-10-19'
        queryWrapper.eq("DATE_FORMAT(order_date,'%Y-%m')",year + "-" + (month < 10 ? "0" : "") + month);
        List<OrderSetting> list = this.list(queryWrapper);//查询结果
        Map<String, Map<String, Integer>> data = new HashMap<>();
        for(OrderSetting orderSetting : list){
            Map<String, Integer> map = new HashMap<>();
            map.put("number",orderSetting.getOrderNumber());
            map.put("reservations",orderSetting.getOrdered());
            //data.put('2020-10-19',{number:200,reservations:200})
            data.put(DateUtil.formatDate(orderSetting.getOrderDate()),map);
        }
        return data;
    }

    /**
     * @param day 具体预约日期
     * @param num 可预约总数
     * @return
     */
    @Override
    public boolean daySetting(String day, Integer num) {
        OrderSetting orderSetting = findByDate(day);
        if(orderSetting == null){
            orderSetting = new OrderSetting();
        }
        orderSetting.setOrderDate(DateUtil.parseDate(day));
        orderSetting.setOrderNumber(num);
        return this.saveOrUpdate(orderSetting);
    }
}
